package com.aliyun.apsara.alivclittlevideo.view.video.videolist;

import android.content.Context;
import android.graphics.Color;
import android.graphics.SurfaceTexture;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.aliyun.apsara.alivclittlevideo.R;
import com.aliyun.apsara.alivclittlevideo.constants.LittleVideoParamConfig;
import com.aliyun.apsara.alivclittlevideo.net.data.LittleMineVideoInfo;
import com.aliyun.player.AliListPlayer;
import com.aliyun.player.AliPlayerFactory;
import com.aliyun.player.IPlayer;
import com.aliyun.player.bean.ErrorCode;
import com.aliyun.player.bean.ErrorInfo;
import com.aliyun.player.bean.InfoBean;
import com.aliyun.player.nativeclass.CacheConfig;
import com.aliyun.player.nativeclass.MediaInfo;
import com.aliyun.player.nativeclass.PlayerConfig;
import com.aliyun.player.nativeclass.TrackInfo;
import com.aliyun.player.source.StsInfo;
import com.aliyun.player.source.UrlSource;
import com.aliyun.player.source.VidSts;
import com.aliyun.svideo.common.utils.ToastUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 视频列表view
 *
 * @author xlx
 */
public class AlivcVideoListView extends FrameLayout {
    private static String TAG = AlivcVideoListView.class.getSimpleName();
    private Context mContext;
    private RecyclerViewEmptySupport recycler;
    private SwipeRefreshLayout refreshView;
    private BaseVideoListAdapter adapter;
    private PagerLayoutManager pagerLayoutManager;

    private View mPlayerViewContainer;
    private ImageView mPlayIcon;
    /**
     * 数据是否到达最后一页
     */
    private boolean isEnd;
    /**
     * 播放器的封装，可以提前准备视频
     */
    private AliListPlayer mListPlayer;
    private TextureView mTextureView;
    private List<IVideoSourceModel> list;

    /**
     * 刷新数据listener
     */
    private OnRefreshDataListener onRefreshDataListener;

    private OnPlayStatusListener mOnPlayStatusListener;
    private OnAdStatusListener mOnAdStatusListener;

    private OnPlayProgressListener mOnPlayProgressListener;

    /**
     * 判断是否处于加载数据的状态中
     */
    private boolean isLoadingData;
    /**
     * 预加载位置, 默认离底部还有5条数据时请求下一页视频列表
     */
    private static final int DEFAULT_PRELOAD_NUMBER = 5;
    /**
     * 是否点击暂停
     */
    private boolean isPause = false;
    /**
     * 当前页面是否处于可见状态
     */
    private boolean isOnBackground = false;
    /**
     * 当前选中位置
     */
    private int mCurrentPosition;
    /**
     * 正常滑动，上一个被暂停的位置
     */
    private int mLastStopPosition = -1;

    PlayStatusChangeListener listener;

    private boolean isCompletion;

    public AlivcVideoListView(@NonNull Context context) {
        super(context);
        this.mContext = context;
        initPlayer();
        init();
    }

    public void setPlayerChangeListener(PlayStatusChangeListener listener) {
        this.listener = listener;
    }

    private AlivcVideoListView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        throw new IllegalArgumentException("this view isn't allow create by xml");
    }

    private GestureDetector gestureDetector;

    /**
     * 初始化播放器相关
     */
    private void initPlayer() {
        mPlayerViewContainer = View.inflate(getContext(), R.layout.layout_player_view, null);
        mTextureView = mPlayerViewContainer.findViewById(R.id.video_textureview);
        mPlayIcon = mPlayerViewContainer.findViewById(R.id.iv_play_icon);
        gestureDetector = new GestureDetector(mContext,
                new GestureDetector.SimpleOnGestureListener() {

                    @Override
                    public boolean onSingleTapConfirmed(MotionEvent e) {
                        //判断当前view是否可见，防止退后台、切换页面和单击同时操作导致，退后台时视频又重新开始播放
                        if (AlivcVideoListView.this.isShown()) {
                            onPauseClick();
                        }
                        return true;
                    }

                    @Override
                    public boolean onDown(MotionEvent e) {
                        return true;
                    }

                    @Override
                    public boolean onDoubleTap(MotionEvent e) {
                        return super.onDoubleTap(e);
                    }

                    @Override
                    public void onLongPress(MotionEvent e) {
                        super.onLongPress(e);
                        if (mOnGestureDetectorListener != null) {
                            mOnGestureDetectorListener.onLongPress(e);
                        }
                    }
                });
        mPlayerViewContainer.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return gestureDetector.onTouchEvent(event);
            }
        });
        mTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
                Surface mSurface = new Surface(surface);
                if (mListPlayer != null) {
                    mListPlayer.setSurface(mSurface);
                    mListPlayer.redraw();
                }
            }

            @Override
            public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
                if (mListPlayer != null) {
                    mListPlayer.redraw();
                }
            }

            @Override
            public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
                return true;
            }

            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture surface) {

            }
        });
        mListPlayer = AliPlayerFactory.createAliListPlayer(mContext);
        //打开播放器
        mListPlayer.enableLog(LittleVideoParamConfig.Player.LOG_ENABLE);
        PlayerConfig config = mListPlayer.getConfig();
        config.mClearFrameWhenStop = true;
        config.mMaxBufferDuration = 10000;
        config.mStartBufferDuration = 200;
        mListPlayer.setConfig(config);
        mListPlayer.enableLog(true);
        mListPlayer.setLoop(true);
        mListPlayer.setAutoPlay(true);

        //添加缓存配置，现在列表播器开启缓存会循环播放会出现黑屏闪烁，暂时关闭缓存
        CacheConfig cacheConfig = new CacheConfig();
        cacheConfig.mEnable = true;
        cacheConfig.mDir = LittleVideoParamConfig.DIR_CACHE;
        cacheConfig.mMaxSizeMB = 100;
        cacheConfig.mMaxDurationS = 30;
        mListPlayer.setCacheConfig(cacheConfig);
        //mListPlayer.set
        //指定播放清晰度
        mListPlayer.setDefinition(VideoQuality.PLAY.getValue());
        mListPlayer.setOnPreparedListener(new IPlayer.OnPreparedListener() {
            @Override
            public void onPrepared() {
                MediaInfo mediaInfo = mListPlayer.getMediaInfo();
                List<TrackInfo> trackInfos = mediaInfo.getTrackInfos();
                for (TrackInfo trackInfo : trackInfos) {
                    float aspectRatio = (float) trackInfo.getVideoWidth() / trackInfo.getVideoHeight();
                    if (aspectRatio < 9f / 15f) {
                        mListPlayer.setScaleMode(IPlayer.ScaleMode.SCALE_ASPECT_FILL);
                    } else {
                        mListPlayer.setScaleMode(IPlayer.ScaleMode.SCALE_ASPECT_FIT);
                    }
                    break;
                }
                isCompletion = false;
                if (!isPause && !isOnBackground) {
                    mListPlayer.start();
                    if (mOnPlayStatusListener != null) {
                        mOnPlayStatusListener.start();
                        mOnAdStatusListener.VideoStart();
                    }
                }
            }
        });
        mListPlayer.setOnRenderingStartListener(new IPlayer.OnRenderingStartListener() {
            @Override
            public void onRenderingStart() {
                //  2018/11/21 隐藏封面
                BaseVideoListAdapter.BaseHolder holder = (BaseVideoListAdapter.BaseHolder) recycler.findViewHolderForLayoutPosition(mCurrentPosition);
                if (holder != null) {
                    holder.getCoverView().setVisibility(GONE);
                }
                if (mLoadingListener != null) {
                    mLoadingListener.onLoadingEnd();
                }
            }
        });

        mListPlayer.setOnStateChangedListener(new IPlayer.OnStateChangedListener() {
            @Override
            public void onStateChanged(int i) {
                Log.d("TAG", i + "");
                if (mLoadingListener != null) {
                    if (i == IPlayer.started || i == IPlayer.completion) {
                        mLoadingListener.onLoadingEnd();
                    } else if (i == IPlayer.initalized || i == IPlayer.prepared) {
                        mLoadingListener.onLoadingBegin();
                    }
                }
            }
        });
        mListPlayer.setOnInfoListener(new IPlayer.OnInfoListener() {
            @Override
            public void onInfo(InfoBean infoBean) {
                Log.d("TAG", "infoBean");
                if (infoBean != null && infoBean.getCode() != null && infoBean.getCode().getValue() == 2) {
                    if (mOnPlayProgressListener != null) {
                        mOnPlayProgressListener.onPlayProgress(infoBean.getExtraValue(), mListPlayer.getDuration());
                    }
                    if (infoBean.getExtraValue() == mListPlayer.getDuration()) {
                        isCompletion = true;
                        if (mOnPlayStatusListener != null) {
                            mOnPlayStatusListener.onCompletion();
                            mOnAdStatusListener.VideoEnd();
                        }
                    }
                }
            }
        });

        mListPlayer.setOnLoadingStatusListener(new IPlayer.OnLoadingStatusListener() {
            @Override
            public void onLoadingBegin() {
                if (mLoadingListener != null) {
                    mLoadingListener.onLoadingBegin();
                }
            }

            @Override
            public void onLoadingProgress(int i, float v) {
                if (mLoadingListener != null) {
                    mLoadingListener.onLoadingProgress(i, v);
                }
            }

            @Override
            public void onLoadingEnd() {
                if (mLoadingListener != null) {
                    mLoadingListener.onLoadingEnd();
                }
            }
        });
        mListPlayer.setOnErrorListener(new IPlayer.OnErrorListener() {
            @Override
            public void onError(ErrorInfo errorInfo) {
                Log.i(TAG, "鉴权过期" + errorInfo.getMsg());
                if (errorInfo.getCode() == ErrorCode.ERROR_SERVER_POP_UNKNOWN) {
                    //鉴权过期
                    if (mTimeExpiredErrorListener != null) {
                        mTimeExpiredErrorListener.onTimeExpiredError();
                        Log.i(TAG, "刷新鉴权");
                    }
                }
                ToastUtils.show(getContext(), errorInfo.getMsg());
            }
        });
    }


    /**
     * 鉴权过期时发生
     */
    private OnTimeExpiredErrorListener mTimeExpiredErrorListener;

    public void setTimeExpiredErrorListener(
            OnTimeExpiredErrorListener mTimeExpiredErrorListener) {
        this.mTimeExpiredErrorListener = mTimeExpiredErrorListener;
    }

    private void init() {
        list = new ArrayList<>();
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_video_list, this, true);
        recycler = view.findViewById(R.id.recycler);
        refreshView = view.findViewById(R.id.refresh_view);

        refreshView.setColorSchemeColors(Color.YELLOW, Color.GREEN, Color.BLUE, Color.RED);
        refreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (onRefreshDataListener != null) {
                    isLoadingData = true;
                    onRefreshDataListener.onRefresh();
                }
            }
        });
        recycler.setHasFixedSize(true);
        pagerLayoutManager = new PagerLayoutManager(mContext);
        pagerLayoutManager.setItemPrefetchEnabled(true);
        recycler.setLayoutManager(pagerLayoutManager);
        //recycler.setEmptyView(view.findViewById(R.id.ll_empty_view));
        pagerLayoutManager.setOnViewPagerListener(new PagerLayoutManager.OnViewPagerListener() {
            @Override
            public void onInitComplete() {
                Log.e(TAG, "onInitComplete mCurrentPosition= " + mCurrentPosition);
                int position = pagerLayoutManager.findFirstVisibleItemPosition();
                if (position != -1) {
                    mCurrentPosition = position;
                }
                int itemCount = adapter.getItemCount();
                if (itemCount - position < DEFAULT_PRELOAD_NUMBER && !isLoadingData && !isEnd) {
                    // 正在加载中, 防止网络太慢或其他情况造成重复请求列表
                    isLoadingData = true;
                    loadMore();
                }
                startPlay(mCurrentPosition);
                mLastStopPosition = -1;
                Log.e(TAG, "onInitComplete mCurrentPosition= " + mCurrentPosition);

            }

            @Override
            public void onPageRelease(boolean isNext, int position) {

                if (mCurrentPosition == position) {
                    mLastStopPosition = position;
                    if (adapter.getDataList().size() > position) {
                        LittleMineVideoInfo.VideoListBean viewSourceModel = (LittleMineVideoInfo.VideoListBean) adapter.getDataList().get(position);
                        stopPlay(viewSourceModel, position);
                    }
                    BaseVideoListAdapter.BaseHolder holder = (BaseVideoListAdapter.BaseHolder) recycler.findViewHolderForLayoutPosition(position);
                    if (holder != null) {
                        holder.getCoverView().setVisibility(VISIBLE);
                    }
                }

            }

            @Override
            public void onPageSelected(int position, boolean b) {
                //重新选中视频不播放，如果该位置被stop过则会重新播放视频
                if (mCurrentPosition == position && mLastStopPosition != position) {
                    return;
                }

                int itemCount = adapter.getItemCount();
                if (itemCount - position < DEFAULT_PRELOAD_NUMBER && !isLoadingData && !isEnd) {
                    // 正在加载中, 防止网络太慢或其他情况造成重复请求列表
                    isLoadingData = true;
                    loadMore();
                }
                if (itemCount == position + 1 && isEnd) {
                    Toast.makeText(getContext(), R.string.alivc_little_play_tip_last_video, Toast.LENGTH_SHORT).show();
                }
                //开始播放选中视频
                startPlay(position);
                mCurrentPosition = position;

                // 自动播放
                if (mViewPagerListener != null) {
                    mViewPagerListener.onPageSelected(mCurrentPosition);
                }
            }
        });
    }

    private OnViewPagerListener mViewPagerListener;

    public interface OnViewPagerListener {
        void onPageSelected(int position);
    }

    public void setOnViewPagerListener(OnViewPagerListener listener) {
        this.mViewPagerListener = listener;
    }


    public int getCurrentPosition() {
        return mCurrentPosition;
    }

    public IVideoSourceModel getCurrentViewModel() {
        return list.get(mCurrentPosition);
    }

    public void playNext() {
        int tempCurrentPostion = mCurrentPosition + 1;
        if (tempCurrentPostion < list.size()) {
            stopPlay((LittleMineVideoInfo.VideoListBean) list.get(mCurrentPosition), mCurrentPosition);
            recycler.smoothScrollToPosition(tempCurrentPostion);
            adapter.notifyDataSetChanged();
        }
    }

    public void playAd() {
        // 6 index
        if (5 < list.size()) {
            stopPlay((LittleMineVideoInfo.VideoListBean) list.get(mCurrentPosition), mCurrentPosition);
            recycler.smoothScrollToPosition(5);
            adapter.notifyDataSetChanged();
        }
    }

    /**
     * 停止视频播放
     */
    private void stopPlay(LittleMineVideoInfo.VideoListBean videoListBean, int position) {
        ViewParent parent = mPlayerViewContainer.getParent();
        if (parent != null) {
            ((ViewGroup) parent).removeView(mPlayerViewContainer);
        }
        mListPlayer.stop();
        mListPlayer.setSurface(null);

        if (mOnPlayStatusListener != null) {
            mOnPlayStatusListener.stop(videoListBean, position);
            mOnAdStatusListener.VideoStop();
        }
    }

    public AliListPlayer getPlayer() {
        return mListPlayer;
    }

    /**
     * 开始播放
     *
     * @param position 播放位置
     */
    private void startPlay(int position) {
        if (position < 0 || position > list.size()) {
            return;
        }
        IVideoSourceModel video = list.get(position);
        //恢复界面状态
        mPlayIcon.setVisibility(View.GONE);
        isPause = false;
        BaseVideoListAdapter.BaseHolder holder = (BaseVideoListAdapter.BaseHolder) recycler.findViewHolderForLayoutPosition(position);
        ViewParent parent = mPlayerViewContainer.getParent();
        if (parent != null) {
            ((ViewGroup) parent).removeView(mPlayerViewContainer);
        }
        if (holder != null) {
            holder.getContainerView().addView(mPlayerViewContainer, 0);
        }
        //防止退出后台之后，再次调用start方法，导致视频播放
        if (video.getSourceType() == VideoSourceType.TYPE_STS) {
            VidSts vidSts = video.getVidStsSource();
            StsInfo stsInfo = new StsInfo();
            if (vidSts != null) {
                stsInfo.setAccessKeyId(vidSts.getAccessKeyId());
                stsInfo.setAccessKeySecret(vidSts.getAccessKeySecret());
                stsInfo.setSecurityToken(vidSts.getSecurityToken());
                stsInfo.setRegion(vidSts.getRegion());
            }
            if (position - mCurrentPosition == 1) {
                mListPlayer.moveToNext(stsInfo);
            } else if (position - mCurrentPosition == -1) {
                mListPlayer.moveToPrev(stsInfo);
            } else {
                mListPlayer.moveTo(video.getUUID(), stsInfo);
            }
        } else if (video.getSourceType() == VideoSourceType.TYPE_URL) {
            if (video.getType() != 100) {
                mListPlayer.moveTo(video.getUUID());
            } else if (!isPause && !isOnBackground) {
                if (mOnAdPlayListener != null && mOnAdPlayListener.isAdStart() && mOnPlayStatusListener != null) {
                    mOnPlayStatusListener.start();
                }
            }
        } else if (video.getSourceType() == VideoSourceType.TYPE_LIVE) {
            UrlSource urlSource = video.getUrlSource();
            mListPlayer.setDataSource(urlSource);
            mListPlayer.prepare();
        }
    }

    public boolean isRefreshing() {
        if (refreshView != null) {
            return refreshView.isRefreshing();
        }
        return false;
    }

    public void onRefresh() {
        if (onRefreshDataListener != null) {
            isLoadingData = true;
            onRefreshDataListener.onRefresh();
        }
        if (refreshView != null) {
            refreshView.setRefreshing(true);
        }
    }

    /**
     * 加载更多
     */
    private void loadMore() {
        if (onRefreshDataListener != null) {
            onRefreshDataListener.onLoadMore();
        }
    }

    /**
     * 刷新数据
     *
     * @param list 刷新数据
     */
    public void refreshData(List<IVideoSourceModel> list) {
        clearNotShowVideo(list);
        if (refreshView != null && refreshView.isRefreshing()) {
            refreshView.setRefreshing(false);
            // 加载完毕, 重置加载状态
        }
        if (mListPlayer != null) {
            mListPlayer.clear();
            for (IVideoSourceModel iVideoSourceModel : list) {
                if (iVideoSourceModel.getSourceType() == VideoSourceType.TYPE_STS) {
                    mListPlayer.addVid(iVideoSourceModel.getVidStsSource().getVid(), iVideoSourceModel.getUUID());
                } else if (iVideoSourceModel.getSourceType() == VideoSourceType.TYPE_URL) {
                    String url = iVideoSourceModel.getUrlSource().getUri();
                    String uuid = iVideoSourceModel.getUUID();
                    mListPlayer.addUrl(url, uuid);
                }
            }
        }
        isEnd = false;
        isLoadingData = false;
        adapter.refreshData(list);
    }

    public List<IVideoSourceModel> getData() {
        if (list != null) {
            return list;
        }
        return new ArrayList<>();
    }

    public void closeRefresh() {
        isLoadingData = false;
        refreshView.setRefreshing(false);
    }

    /**
     * 刷新数据，并播放指定位置的视频
     *
     * @param list     视频列表数据
     * @param position 刷新完成之后播放位置
     */
    public void refreshData(List<IVideoSourceModel> list, int position) {
        int size = list.size();
        if (position < 0) {
            position = 0;
        }
        if (size <= position) {
            position = size - 1;
        }
        //获取不进行显示
        int notShowVideoCount = 0;
        for (int i = 0; i < size; i++) {
            if (i < position && list.get(i).getSourceType() == VideoSourceType.TYPE_ERROR_NOT_SHOW) {
                notShowVideoCount++;
            }
        }
        mCurrentPosition = position - notShowVideoCount;
        refreshData(list);
        recycler.scrollToPosition(mCurrentPosition);

    }

    /**
     * 清除不允许显示的视频
     *
     * @param list
     */
    private void clearNotShowVideo(List<IVideoSourceModel> list) {
        Iterator<IVideoSourceModel> it = list.iterator();
        while (it.hasNext()) {
            if (it.next().getSourceType() == VideoSourceType.TYPE_ERROR_NOT_SHOW) {
                it.remove();
            }
        }
    }

    /**
     * 加载更多数据
     *
     * @param list
     */
    public void addMoreData(List<IVideoSourceModel> list) {
        if (list == null || list.size() < 1) {
            isEnd = true;
        } else {
            isEnd = false;
        }
        clearNotShowVideo(list);
        isLoadingData = false;

        if (adapter != null) {
            adapter.addMoreData(list);
        }
        if (mListPlayer != null) {
            for (IVideoSourceModel iVideoSourceModel : list) {
                if (iVideoSourceModel.getSourceType() == VideoSourceType.TYPE_STS) {
                    mListPlayer.addVid(iVideoSourceModel.getVidStsSource().getVid(), iVideoSourceModel.getUUID());
                } else if (iVideoSourceModel.getSourceType() == VideoSourceType.TYPE_URL) {
                    mListPlayer.addUrl(iVideoSourceModel.getUrlSource().getUri(), iVideoSourceModel.getUUID());
                }
            }
        }
        if (refreshView.isRefreshing()) {
            refreshView.setRefreshing(false);
        }

    }

    /**
     * @param model
     */
    public void addAdPlayer(IVideoSourceModel model) {
        if (model != null && mListPlayer != null) {
            mListPlayer.addUrl(model.getUrlSource().getUri(), model.getUUID());
        }
    }


    private IPlayer.OnLoadingStatusListener mLoadingListener;

    public void setLoadingListener(IPlayer.OnLoadingStatusListener mLoadingListener) {
        this.mLoadingListener = mLoadingListener;
    }

    public void setOnRefreshDataListener(OnRefreshDataListener onRefreshDataListener) {
        this.onRefreshDataListener = onRefreshDataListener;
    }

    /**
     * 刷新数据
     */
    public interface OnRefreshDataListener {
        /**
         * 下拉刷新
         */
        void onRefresh();

        /**
         * 上拉加载
         */
        void onLoadMore();
    }

    public interface OnPlayStatusListener {

        void start();

        void stop(IVideoSourceModel videoListBean, int position);

        void onCompletion();
    }

    public interface OnAdStatusListener {

        void VideoStart();

        void VideoStop();

        void VideoEnd();
    }


    public interface OnGestureDetectorListener {
        void onLongPress(MotionEvent e);
    }

    private OnGestureDetectorListener mOnGestureDetectorListener;

    public void setOnGestureDetectorListener(OnGestureDetectorListener listener) {
        mOnGestureDetectorListener = listener;
    }

    public void setOnPlayStatusListener(OnPlayStatusListener listener) {
        mOnPlayStatusListener = listener;
    }

    public OnPlayStatusListener getOnPlayStatusListener() {
        return mOnPlayStatusListener;
    }

    public void setOnAdListener(OnAdStatusListener listener) {
        mOnAdStatusListener = listener;
    }

    public OnAdStatusListener getOnAdListener() {
        return mOnAdStatusListener;
    }


    public interface OnPlayProgressListener {
        void onPlayProgress(long progress, long all);
    }

    public void setOnPlayProgressListener(OnPlayProgressListener listener) {
        mOnPlayProgressListener = listener;
    }

    public OnPlayProgressListener getOnPlayProgressListener() {
        return mOnPlayProgressListener;
    }

    public interface OnAdPlayListener {
        boolean isAdStart();
    }

    private OnAdPlayListener mOnAdPlayListener;

    public void setOnAdPlayListener(OnAdPlayListener listener) {
        mOnAdPlayListener = listener;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mListPlayer.release();
    }

    /**
     * 暂停播放
     */
    private void pausePlay() {
        isPause = true;
        mPlayIcon.setVisibility(View.VISIBLE);
        mListPlayer.pause();

        if (mOnPlayStatusListener != null) {
            try {
                mOnPlayStatusListener.stop(list.get(mCurrentPosition), mCurrentPosition);
            } catch (Exception e) {
            }
        }
    }

    /**
     * 恢复播放
     */
    public void resumePlay() {
        mPlayIcon.setVisibility(View.GONE);
        if (isPause) {
            mListPlayer.start();
            if (mOnPlayStatusListener != null && !isCompletion) {
                mOnPlayStatusListener.start();
            }
        } else {
            mListPlayer.prepare();
        }
        isPause = false;
    }

    /**
     * activity不可见或者播放页面不可见时调用该方法
     */
    public void setOnBackground(boolean isOnBackground) {
        this.isOnBackground = isOnBackground;
        if (isOnBackground) {
            pausePlay();
        } else {
            resumePlay();
        }
    }

    public boolean isOnBackground() {
        return isOnBackground;
    }

    public void setOnBackgroundAD(boolean isOnBackground) {
        this.isOnBackground = isOnBackground;
    }

    /**
     * 视频暂停/恢复的时候使用，
     */
    public void onPauseClick() {
        if (isPause) {
            resumePlay();
            if (listener != null) {
                listener.startPlay();
            }
        } else {
            pausePlay();
            if (listener != null) {
                listener.pausePlay();
            }
        }
    }

    public boolean isPause() {
        return isPause;
    }

    /**
     * 设置播放器数量
     *
     * @param playerCount
     */
    public void setPlayerCount(int playerCount) {

        mListPlayer.setPreloadCount(playerCount);
    }

    /**
     * 设置adapter
     *
     * @param adapter
     */
    public void setAdapter(BaseVideoListAdapter adapter) {
        this.adapter = adapter;
        recycler.setAdapter(adapter);
        this.list = adapter.getDataList();
    }

    /**
     * 移除当前播放的视频
     */
    public void removeCurrentPlayVideo() {
        try {
            stopPlay((LittleMineVideoInfo.VideoListBean) list.get(mCurrentPosition), mCurrentPosition);
            int tempCurrentPostion = mCurrentPosition;
            //当前视频如果为最后一个视频，则需要播放上一个视频
            if (tempCurrentPostion == list.size() - 1 && list.size() >= 2) {
                recycler.scrollToPosition(tempCurrentPostion - 1);
            }
            list.remove(tempCurrentPostion);
            adapter.notifyDataSetChanged();
        } catch (Exception ignored) {

        }

    }

    /**
     * 视频列表获取失败
     */
    public void loadFailure() {
        if (refreshView.isRefreshing()) {
            refreshView.setRefreshing(false);
        }
    }

    public RecyclerViewEmptySupport getRecycler() {
        return recycler;
    }


    public void performShareClick() {
        if (recycler != null) {
            for (int i = 0; i < recycler.getChildCount(); i++) {
                View childView = recycler.getChildAt(i);
                if (childView != null) {
                    View shareView = childView.findViewWithTag("share");
                    if (shareView != null) {
                        shareView.performClick();
                        break;
                    }
                }
            }
        }
    }

    public void autoPraise() {
        if (recycler != null) {
            for (int i = 0; i < recycler.getChildCount(); i++) {
                View childView = recycler.getChildAt(i);
                if (childView != null) {
                    View shareView = childView.findViewWithTag("praise");
                    if (shareView != null) {
                        shareView.performClick();
                        break;
                    }
                }
            }
        }
    }

}
